<template>
  <div class="Detail">
    <div class="header">
      <div class="left">
        <i class="iconfont iconjiantou2"></i>
      </div>
      <div class="center">
        <i class="iconfont iconnew"></i>
      </div>
      <div class="right">
        <div class="follow" v-if="info.has_follow" @click="unzan">已关注</div>
        <div class="follow" v-else @click="zan">关注</div>
      </div>
    </div>
    <div class="contain">
      <div class="title line2">
        {{ info.title }}
      </div>
      <div class="userinfo">
        <span class="nickname">{{ info.user.nickname }}</span>
        <span class="date">{{ info.create_date | date }}</span>
      </div>
      <div v-html="info.content" v-if="info.type == 1" class="imgs"></div>
      <video :src="info.content" v-else controls></video>
      <div class="good">
        <div
          class="checkgoods"
          :class="{ active: info.has_like }"
          @click="checklike"
        >
          <i class="iconfont icondianzan"></i>
        </div>
      </div>
    </div>
    <div ref="box"></div>
    <div class="comments">
      <div class="comment" v-for="item in comments" :key="item.id">
        <div class="head">
          <div class="left">
            <img :src="$axios.defaults.baseURL + item.user.head_img" alt="" />
          </div>
          <div class="center">
            <div class="nickname">{{ item.user.nickname }}</div>
            <div class="date">{{ item.create_date | date }}</div>
          </div>
          <div class="right" @click="reply(item.user.nickname, item.id)">
            回复
          </div>
        </div>
        <comment
          v-if="item.parent"
          :comment="item"
          :number="getnumber(item, 0)"
        ></comment>
        <div class="bottom">{{ item.content }}</div>
      </div>
    </div>
    <div class="footer">
      <div class="input" v-if="!isshow">
        <div class="left">
          <input type="text" placeholder="发评论" @focus="areashow" />
        </div>
        <div class="center">
          <van-icon name="chat-o" :badge="info.comment_length" size="30" />
        </div>
        <div class="right"><van-icon name="star-o" size="30" /></div>
      </div>
      <div class="textarea" v-else>
        <div class="left">
          <textarea
            name=""
            id=""
            @blur="textshow"
            ref="textarea"
            v-model="content"
            :placeholder="'回复:' + nickname"
          ></textarea>
        </div>
        <div class="right">
          <div @mousedown="send">发送</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: { user: "" },
      comments: [],
      isshow: false,
      content: "",
      nickname: "",
      id: "",
    };
  },
  async created() {
    this.getinfo();
    this.getcomments();
    this.$bus.$on("reply", async (nickname, id) => {
      const token = localStorage.getItem("token");
      if (token) {
        this.isshow = true;
        this.content = "";
        await this.$nextTick();
        this.$refs.textarea && this.$refs.textarea.focus();
        this.nickname = nickname;
        this.id = id;
        console.log(nickname, id);
      } else {
        this.$router.push({
          name: "login",
          params: {
            back: true,
          },
        });
      }
    });
  },
  methods: {
    async getinfo() {
      const res = this.$route;
      const answer = await this.$axios.get(`/post/${res.params.id}`);
      this.info = answer.data.data;
      console.log(this.info, this.info.id);
    },
    async zan() {
      console.log(123);
      const token = localStorage.getItem("token");
      if (token) {
        const res = await this.$axios.get(`/user_follows/${this.info.user.id}`);
        console.log(res);
      } else {
        console.log("没有token");
        this.$router.push({
          name: "login",
          params: {
            back: true,
          },
        });
        return;
      }
      this.getinfo();
    },
    async unzan() {
      const res = await this.$axios.get(`/user_unfollow/${this.info.user.id}`);
      console.log(res);
      this.getinfo();
    },
    async checklike() {
      console.log(123);
      const token = localStorage.getItem("token");
      if (token) {
        const res = await this.$axios.get(`/post_like/${this.info.id}`);
        console.log(res);
        this.getinfo();
      } else {
        this.$router.push({ name: "login", params: { back: true } });
      }
    },
    async getcomments() {
      const res = await this.$axios.get(
        `/post_comment/${this.$route.params.id}`
      );
      this.comments = res.data.data;
      console.log(this.comments);
    },
    getnumber(item, num) {
      if (item.parent) {
        num++;
        return this.getnumber(item.parent, num);
      } else {
        return num;
      }
    },
    getuserinfo(nickname, id) {
      console.log(nickname, id);
      this.isshow = true;
      this.nickname = nickname;
      this.id = id;
    },
    async reply(nickname, id) {
      this.$bus.$emit("reply", nickname, id);
    },
    async areashow() {
      this.isshow = true;
      await this.$nextTick();
      console.log(this.$refs);
      this.$refs.textarea.focus();
    },
    textshow() {
      this.isshow = false;
      if (this.content) {
        return;
      } else {
        this.nickname = "";
        this.id = "";
        this.content = "";
      }
    },
    async send() {
      console.log(123);
      console.log(this.$route.params.id);
      const res = await this.$axios.post(
        `/post_comment/${this.$route.params.id}`,
        {
          content: this.content,
          parent_id: this.id,
        }
      );
      const { message, statusCode } = res.data;
      if (statusCode == 200) {
        this.$toast.success(message);
        this.id = "";
        this.content = "";
        this.nickname = "";
        this.getcomments();
        this.getinfo();
        this.$refs.box.scrollIntoView();
      }
    },
  },
};
</script>

<style scoped lang='less'>
.Detail {
  .header {
    height: 40px;
    display: flex;
    align-items: center;
    padding: 10px;
    box-sizing: border-box;
    border-bottom: 1px solid black;
    border-top: 1px solid black;
    .left {
      width: 30px;
      background-color: #fff;
    }
    .right {
      width: 60px;
      height: 16px;
      border: 1px solid black;
      border-radius: 8px;
      text-align: center;
      line-height: 16px;
    }
    .center {
      padding: 10px;
      flex: 1;
      i {
        font-size: 40px;
      }
    }
  }
  .contain {
    padding: 10px;
    border-bottom: black 3px solid;
    .title {
      font-size: 14px;
      line-height: 30px;
      font-weight: 700;
      padding-bottom: 30px;
      background: pink;
    }
    .userinfo {
      background-color: yellow;
      padding-bottom: 10px;
      .nickname {
        padding: 10px;
      }
    }
    video {
      width: 100%;
    }
    /deep/ img {
      width: 100%;
    }
    .good {
      display: flex;
      // background-color: red;
      justify-content: flex-end;
      align-items: center;
      height: 40px;

      .checkgoods {
        text-align: right;
        border: 1px solid black;
        height: 20px;
        width: 60px;
        border-radius: 20px;
        text-align: center;
      }
      .active {
        border: 1px solid red;
        color: red;
      }
    }
  }
  .comments {
    padding: 10px;
    border-bottom: 1px #999 solid;
    padding-bottom: 50px;
    .comment {
      .head {
        display: flex;
        align-items: center;
        .left {
          img {
            height: 40px;
            width: 40px;
            object-fit: cover;
            border-radius: 50%;
          }
        }
        .center {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          padding-left: 10px;
          height: 50px;
          line-height: 25px;
          .nickname {
            font-size: 16px;
          }
          .date {
            font-size: 13px;
            color: #999;
          }
        }
        .right {
          width: 40px;
          font-size: 13px;
          color: #999;
          height: 30px;
          text-align: center;
          line-height: 30px;
          text-align: center;
        }
      }
      .bottom {
        font-size: 15px;
        padding-bottom: 20px;
        padding-top: 10px;
      }
    }
  }
  .footer {
    border-top: 3px solid black;
    padding: 10px;
    position: fixed;
    bottom: 0;
    background-color: #fff;
    width: 100%;
    height: 40px;
    .input {
      display: flex;
      height: 40px;
      align-items: center;
      .left {
        flex: 2;
        display: flex;
        align-items: center;
        input {
          height: 20px;
          width: 200px;
          border-radius: 10px;
          border: none;
          background-color: #ddd;
          text-indent: 10px;
        }
      }
      .center {
        flex: 1;
        text-align: center;
      }
      .right {
        flex: 1;
      }
    }
    .textarea {
      display: flex;
      .left {
        flex: 3;
        textarea {
          resize: none;
          height: 40px;
          width: 90%;
          border-radius: 5px;
          border: none;
          background-color: #ddd;
        }
      }
      .right {
        flex: 2;
        display: flex;
        align-items: center;
        div {
          width: 40px;
          background-color: red;
          height: 30px;
          text-align: center;
          line-height: 30px;
          border-radius: 3px;
        }
      }
    }
  }
}
</style>